﻿using System.Windows.Forms;
using Business.Forms;
using DevExpress.XtraEditors;

namespace Business.Controls
{
    public class CommandBase : SimpleButton
    {
        private DevExpress.XtraSplashScreen.SplashScreenManager _splash;
        private bool _ativo = false;
        private Keys _atalho;
        private IForm _form;

        public Keys Atalho
        {
            get
            {
                return _atalho;
            }
            set
            {

                _atalho = value;
                Text = Texto(Text);
            }
        }

        private string Texto(string texto)
        {

            var sAtalho = _atalho.ToString();
            if (sAtalho.Length > 3)
            {
                sAtalho = sAtalho.Substring(0, 3);
            }
            if (texto.Contains("("))
            {

                texto = texto.Substring(0, texto.IndexOf("("))
                    + "(" + sAtalho + ")";
            }
            else
            {
                texto = texto + "(" + sAtalho + ")";
            }
            return texto;
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = Texto(value);
            }
        }

        public IForm Form
        {
            get
            {
                return _form;
            }
            set
            {
                _form = value;
                if (!IsDesignMode)
                {

                    if (Form != null)
                    {

                        Form.Form.KeyDown += FormOnKeyDown;
                        Form.Form.Activated += Form_Activated;
                        _splash = new DevExpress.XtraSplashScreen.SplashScreenManager(Form.Form, typeof(Forms.OperacaoDbWait), true, true);
                    }
                    
                }
                
                
            }
        }

        protected void BeforeOperation()
        {
            if (!_splash.IsSplashFormVisible && _ativo)
            {
                _splash.ShowWaitForm();
            }
            
        }
        protected void AfterOperation()
        {
            if (_splash.IsSplashFormVisible && _ativo)
            {
                _splash.CloseWaitForm();
            }
            
        }
        void Form_Activated(object sender, System.EventArgs e)
        {
            Actived();

            _ativo = true;
        }

        private void FormOnKeyDown(object sender, KeyEventArgs keyEventArgs)
        {
            OnKeyDown(keyEventArgs);
        }

        public CommandBase()
        {
            Height = 35;
            Width = 100;
        }

        protected void ExibeErro(string mensagem)
        {
            MessageBox.Show(mensagem);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // CommandBase
            // 
            this.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.TabStop = false;
            this.ResumeLayout(false);

        }

        public virtual void Actived()
        {
            
        }
    }
}
